package com.wing.system.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.system.model.entity.ApkVersionManager;
import com.wing.system.service.ApkVersionManagerService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Validated
@Api(value = "Admin-apk-版本管理", tags = "#/system/systemApp")
@RestController("apkVersionManagerController")
@RequestMapping("system/admin/apkVersionManager")
public class ApkVersionManagerController {

	private final static String COS1 = "cos1.xgongchang.cn";
	private final static String COS2 = "cos2.xgongchang.cn";

	@Autowired
	private ApkVersionManagerService apkVersionManagerService;

	@ApiOperation(value = "查询客户产品规划列表", notes = "搜索")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
			,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
			,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
			,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
	})
	@GetMapping("/list")
	public PageResult<ApkVersionManager> list(
			@RequestParam(defaultValue = "1") Integer page
			,@RequestParam(defaultValue = "10") Integer limit
			,@RequestParam(defaultValue = "create_time") String sort
			,@RequestParam(defaultValue = "asc") String order
			,String versionName
			,String type
	) {
		Page<ApkVersionManager> pg = new Page<>(page, limit);
		Map paramMap = new HashMap();
		paramMap.put("type", type);
		paramMap.put("versionName", versionName);
		IPage<ApkVersionManager> list = apkVersionManagerService.pageList4app(pg, paramMap);
		return new PageResult<>(list.getRecords(), list.getTotal());
	}


	@ApiOperation(value = "查询版本最新下载信息", notes = "下载-apk信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "String", paramType = "path")
	})
	@GetMapping("/getLastBy/{type}")
	public JsonResult getLastBy(@PathVariable(value = "type") String type) {
		return JsonResult.success(apkVersionManagerService.getLastBy(type));
	}


	@ApiOperation(value = "添加apk版本信息", notes = "添加")
	@RequestObjectFieldIgnore({"id", "createTime", "remark"})
	@PostMapping("/add")
	public JsonResult add(@RequestBody ApkVersionManager apkVersionManager) {
		apkVersionManager.setCreatorName(JwtUtils.getLoginUsername());
		apkVersionManager.setCreator(JwtUtils.getLoginUserId());
		apkVersionManager.setVersionName(apkVersionManager.getVersionName());
		apkVersionManager.setCreateTime(LocalDateTime.now());
		boolean save = apkVersionManagerService.save(apkVersionManager);
		if (save) {
			return JsonResult.success("添加成功");
		}
		return JsonResult.fail("添加失败");
	}

	@ApiOperation(value = "修改apk版本信息", notes = "修改")
	@PostMapping("/update")
	public JsonResult update(@RequestBody ApkVersionManager apkVersionManager) {
		ApkVersionManager info = apkVersionManagerService.getById(apkVersionManager.getId());
		if (info == null) {
			return JsonResult.fail("修改失败！");
		}
		info.setModifierName(JwtUtils.getLoginUsername());
		info.setModifier(JwtUtils.getLoginUserId());
		info.setUpdateTime(LocalDateTime.now());
		if (apkVersionManagerService.updateById(info)) {
			return JsonResult.success("修改成功！");
		}
		return JsonResult.fail("修改失败！");
	}

	@ApiOperation(value = "删除apk版本信息", notes = "删除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "apk_id", required = true, dataType = "Integer", paramType = "path")
	})
	@DeleteMapping("/delete/{id}" )
	public JsonResult delete(@PathVariable("id") Long id) {
		if (apkVersionManagerService.removeById(id)) {
			return JsonResult.success("删除成功");
		}
		return JsonResult.fail("删除失败");
	}
}
